package com.xyy.saas.payment.callback.core.result;

import com.alibaba.fastjson.JSON;
import com.xyy.saas.payment.callback.core.dto.CallbackMsg;
import com.xyy.saas.payment.common.RocketMqTopic;
import com.xyy.saas.payment.util.mq.MqProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @date 2019/11/25 19:27
 */
@Component
public class CallbackRunner {
    @Autowired
    private MqProducer mqProducer;

    public void callback(String tradeNo, String type, Integer amount, Long successTime, String callback, String merchantId, String businessOrderNo) {
        callback(tradeNo, type, amount, 0, successTime, callback, null, null, null, businessOrderNo, merchantId);
    }
    public void callback(String tradeNo, String type, Integer amount, Long successTime, String callback, String merchantId, String businessOrderNo, String msg) {
        callback(tradeNo, type, amount, 0, successTime, callback, null, msg, null, businessOrderNo, merchantId);
    }
    public void callback(String tradeNo, String type, Integer amount, Integer fee, Long successTime, String callback) {
        callback(tradeNo, type, amount, fee, successTime, callback, null, null, null, null, null);
    }
    public void callback(String tradeNo, String type, Integer amount, Integer fee, Long successTime, String callback, String channel) {
        callback(tradeNo, type, amount, fee, successTime, callback, channel, null, null, null, null);
    }
    public void callback(String tradeNo, String type, Integer amount, Integer fee, Long successTime, String callback, String channel, String msg) {
        callback(tradeNo, type, amount, fee, successTime, callback, channel, msg, null, null, null);
    }
    public void callback(String tradeNo, String type, Integer amount, Integer fee, Long successTime, String callback, String channel, String msg, String extend) {
        callback(tradeNo, type, amount, fee, successTime, callback, channel, msg, extend, null, null);
    }
    public void callback(String tradeNo, String type, Integer amount, Integer fee, Long successTime, String callback, String channel, String msg, String extend, String businessOrderNo) {
        callback(tradeNo, type, amount, fee, successTime, callback, channel, msg, extend, businessOrderNo, null);
    }
    public void callback(String tradeNo, String type, Integer amount, Integer fee, Long successTime, String callback, String channel, String msg, String extend, String businessOrderNo, String merchantId) {
        CallbackMsg callbackMsg = new CallbackMsg(tradeNo, type, amount, fee, successTime, callback, channel, msg, extend, businessOrderNo, merchantId);
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_CALLBACK, JSON.toJSONString(callbackMsg));
    }

    /**
     * 发送消息
     * @param callbackMsg
     */
    public void callback(CallbackMsg callbackMsg) {
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_CALLBACK, JSON.toJSONString(callbackMsg));
    }

    /**
     * 根据Topc发送消息
     * @param topic
     * @param callbackMsg
     */
    public void callback(String topic, CallbackMsg callbackMsg) {
        mqProducer.syncSendMsg(topic, JSON.toJSONString(callbackMsg));
    }

    public void callback(Object domain){
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_WX_CALLBACK, JSON.toJSONString(domain));
    }

    public void wxEntrustCallback(Object domain){
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_WX_ENTRUST_CALLBACK, JSON.toJSONString(domain));
    }
    public void wxPapPayCallback(Object domain){
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_WX_PAPPAY_CALLBACK, JSON.toJSONString(domain));
    }

    public void wxPaySuccesscallback(Object domain){
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_WX_CALLBACK, JSON.toJSONString(domain));
    }
    public void aliPaySuccessCallback(Object domain){
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_ALI_CALLBACK, JSON.toJSONString(domain));
    }

    public void aliRefundCallback(String tradeNo, String type, Integer amount, Integer fee, Long successTime, String callback){
        CallbackMsg callbackMsg = new CallbackMsg(tradeNo, type, amount, fee, successTime, callback, null);
        mqProducer.syncSendDelayMsg(RocketMqTopic.TOPIC_CALLBACK, JSON.toJSONString(callbackMsg), 3);
    }

    public void callbackPingXX(Object domain){
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_PINGXX_CALLBACK, JSON.toJSONString(domain));
    }

    public void callbackPingXXWithTag(String tag, Object domain) {
        mqProducer.syncSendMsg(RocketMqTopic.TOPIC_PINGXX_CALLBACK + ":" + tag, JSON.toJSONString(domain));
    }

    public void syncSendMsg(String topic, Object domain){
        mqProducer.syncSendMsg(topic, JSON.toJSONString(domain));
    }

    public void syncSendMsgWithTag(String topic, String tag, Object domain) {
        mqProducer.syncSendMsg(topic + ":" + tag, JSON.toJSONString(domain));
    }

    /**
     * 延迟消息
     */
    public void syncSendDelayMsg(String topic, Object domain, int delayLevel) {
        mqProducer.syncSendDelayMsg(topic, JSON.toJSONString(domain), delayLevel);
    }

    public void syncSendDelayMsgWithTag(String topic, String tag, Object domain, int delayLevel) {
        mqProducer.syncSendDelayMsg(topic + ":" + tag, JSON.toJSONString(domain), delayLevel);
    }

}
